package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.CustomException;
import com.dto.SetmealDto;
import com.entity.Category;
import com.entity.Setmeal;
import com.entity.SetmealDish;
import com.mapper.SetmealMapper;
import com.service.CategoryService;
import com.service.SetmealDishService;
import com.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
//    @Autowired
//    private CategoryService categoryService;
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐信息（包含套菜的信息，和套餐所含菜品的信息）
        this.save(setmealDto);//保存套餐信息
        //获取套餐所含有的菜品
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //获取该套餐的套餐id
        Long dtoId = setmealDto.getId();
        for (SetmealDish s:setmealDishes){
            s.setSetmealId(dtoId);
        }
        setmealDishService.saveBatch(setmealDishes);
    }
     //套餐修改前回显数据
    @Override
    public SetmealDto getWithDish(Long id) {
        Setmeal setmeal = this.getById(id);//查找套餐的基本数据
        LambdaQueryWrapper<SetmealDish> lqw=new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishList = setmealDishService.list(lqw);
//        Long categoryId = setmeal.getCategoryId();
//        Category category = categoryService.getById(categoryId);
//        String categoryName = category.getName();
        SetmealDto setmealDto=new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        setmealDto.setSetmealDishes(setmealDishList);
//        setmealDto.setCategoryName(categoryName);
        return setmealDto;
    }

    @Override
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {

//        System.out.println("-----------------------这个id为"+id+"--------------------------------------");
//        LambdaQueryWrapper<Setmeal> lqw=new LambdaQueryWrapper<>();
//        lqw.eq(Setmeal::getId,id);
//        this.update(lqw);//更改基本数据(错误！更改的话参数得要实体，光加条件不行)
        this.updateById(setmealDto);
        System.out.println("更改基本数据完成！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！");
        //删除包含的的菜品
        Long id = setmealDto.getId();
        LambdaQueryWrapper<SetmealDish> lqw2=new LambdaQueryWrapper<>();
        lqw2.eq(SetmealDish::getSetmealId,id);
        setmealDishService.remove(lqw2);
        System.out.println("删除数据完成!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        //新增选择好的菜品
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish s:setmealDishes){
            s.setSetmealId(id);
        }
        setmealDishService.saveBatch(setmealDishes);
        System.out.println("新增数据完成");
    }

    @Override//批量起售或停售
    public void setStatus(Integer status, List<Long> ids) {
        if (status==1){
            //判断要批量起售
            for (long id: ids){
                Setmeal setmeal = this.getById(id);
                setmeal.setStatus(1);
                this.updateById(setmeal);
            }
        }
        if (status==0){
            //判断要批量停售
            for (long id: ids){
                Setmeal setmeal = this.getById(id);
                setmeal.setStatus(0);
                this.updateById(setmeal);
            }
        }

    }

    @Override//批量删除
    public void deleteByIds(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> lqw=new LambdaQueryWrapper<>();
        lqw.in(Setmeal::getId,ids);
        lqw.eq(Setmeal::getStatus,1);
        long count = this.count(lqw);
        if (count>0){
            throw new CustomException("有套餐是起售状态，删个der");
        }
        removeByIds(ids);//删除选择的id数组指定的套餐
        LambdaQueryWrapper<SetmealDish> lqw2=new LambdaQueryWrapper<>();
        lqw2.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(lqw2);//删除与该套餐相关的套餐菜品信息
    }
}
